package erp_services

import (
	"context"
	"errors"
	"strconv"
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type orderDispatchConfigService struct {
}

var OrderDispatchConfigService = new(orderDispatchConfigService)

// 查询队列
func (orderDispatchConfigService *orderDispatchConfigService) GetOrderDispatchConfig(queue_type int) (orderDispatchConfig []erp_models.OrderDispatchConfig, err error) {
	if queue_type == 1 || queue_type == 0 {
		err = global.App.DB.Where("queue_type = ? ", queue_type).Find(&orderDispatchConfig).Error
	} else {
		err = global.App.DB.Find(&orderDispatchConfig).Error
	}

	if err != nil {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}

// 获取排队顺序
func (orderDispatchConfigService *orderDispatchConfigService) GetOneQueue() (seqQueue erp_models.OrderDispatchConfig, err error) {
	seq := 0
	orderDispatchConfig, err := OrderDispatchConfigService.GetOrderDispatchConfig(1)
	exist := global.App.Redis.Exists(context.Background(), carSeqKey).Val()
	if exist == 1 {
		seq, _ = strconv.Atoi(global.App.Redis.Get(context.Background(), carSeqKey).Val())
		seq = seq + 1

		if seq >= len(orderDispatchConfig) {
			seq = 0
		}
	}
	seqQueue = orderDispatchConfig[seq]
	global.App.Redis.GetSet(context.Background(), carSeqKey, seq)

	return
}
